<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 个人信息 -->
    <input type="file" ref="file" hidden @change="changeFile">
    <van-cell class="avatar-cell" title="头像" @click="$refs.file.click()" is-link center>
      <van-image
        class="avatar"
        round
        fit="cover"
        :src="userInfo.photo"
      />
    </van-cell>
    <van-cell title="昵称" @click="updateNameShow=true" :value="userInfo.name" is-link />
    <van-cell title="性别" @click="updateGenderShow=true" :value="userInfo.gender ? '女':'男'" is-link />
    <van-cell title="生日" @click="updateBirthdayShow=true" :value="userInfo.birthday" is-link />
    <!-- /个人信息 -->

    <!-- 编辑昵称 -->
    <van-popup v-model="updateNameShow" position="bottom" style="height:100%">
      <!-- <update-name :value="userInfo.name" @input="userInfo.name=$event" @close="updateNameShow=false"></update-name> -->
      <update-name v-if="updateNameShow" v-model="userInfo.name" @close="updateNameShow=false"></update-name>
    </van-popup>

    <!-- 编辑性别 -->
    <van-popup v-model="updateGenderShow" position="bottom" style="height:40vh">
      <update-gender v-if="updateGenderShow" v-model="userInfo.gender" @close="updateGenderShow=false"></update-gender>
    </van-popup>

    <!-- 编辑生日 -->
    <van-popup v-model="updateBirthdayShow" position="bottom" style="height:40vh">
      <!-- <update-birthday  :birthday="userInfo.birthday" @update:birthday="userInfo.birthday=$event" @close="updateBirthdayShow=false"></update-birthday> -->
      <update-birthday v-if="updateBirthdayShow"  :birthday.sync="userInfo.birthday" @close="updateBirthdayShow=false"></update-birthday>
    </van-popup>

    <!-- 编辑头像 -->
     <van-popup v-model="updatePhotoShow" position="bottom" style="height:100%" >
      <update-photo  v-if="updatePhotoShow"  @update-photo="userInfo.photo =$event" :img="showImgUrl" @close="updatePhotoShow=false"></update-photo>
    </van-popup>

  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import updateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  components: { updateName,UpdateGender,UpdateBirthday, UpdatePhoto },
  name: 'UserProfile',
  data () {
    return {
      // 1. data定义变量
      userInfo:{},
      updateNameShow:false,
      updateGenderShow:false,
      updateBirthdayShow:false,
      updatePhotoShow:false,
      showImgUrl:''
    }
  },
  created () {
    // 3. created调用
    this.loadUserProfile()
  },
  methods: {
    // 2.methods定义方法
    async loadUserProfile(){
      try {
        let { data } = await getUserProfile()
        this.userInfo = data.data
      } catch (error) {
        return this.$toast.fail('获取用户资料失败')
      }
    },
    // 图片切换事件
    changeFile(){
      // 文件信息
      const file = this.$refs.file.files[0]
      // 类型的判断
      const type =  ['image/png','image/gif','image/jpg','image/jpeg']
      if(!type.includes(file.type)){
        this.$refs.file.value = ''
        return this.$toast.fail('类型不正确')
      }
      // 大小的判断
      if(file.size / 1024 / 1024 >3){
         this.$refs.file.value = ''
         return this.$toast.fail('文件大小不能超过3M')
      }
      // 设置预览地址
      this.showImgUrl = URL.createObjectURL(file)
      // 清空内容
      this.$refs.file.value = ''
      // 打开遮罩层
      this.updatePhotoShow = true
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
  .van-popup{
      background-color:#f5f7f9;
  }  
}
</style>
